package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.clinic.DepartmentData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ：zfr
 * @date ：2021/3/12 15:59
 */
public class ClinicDepartmentDaoImpl extends TempDataObjectDao<DepartmentData> {
    public static String addSql = "insert into t_u_clinic_department(userId, type, level) values(?,?,?)";
    @Override
    protected boolean addData(DepartmentData data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getLevel()));
        if (getDb().executeNoneQuery(addSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateSql = "update t_u_clinic_department set level=? where userId=? and type=?";
    @Override
    protected boolean updateData(DepartmentData data) {
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getLevel()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getType()));
        if (getDb().executeNoneQuery(updateSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected DepartmentData parseData(ResultSet rs) throws SQLException {
        DepartmentData data = new DepartmentData();
        data.setUserId(rs.getLong("userId"));
        data.setType(rs.getInt("type"));
        data.setLevel(rs.getInt("level"));
        return data;
    }

    public Map<Integer, DepartmentData> getClinicDepartmentDataMap(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, DepartmentData> departmentDataMap = new ConcurrentHashMap<>();
        String selectSql = "select * from t_u_clinic_department where userId = ?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(selectSql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                DepartmentData departmentData = parse(rs);
                departmentDataMap.put(departmentData.getType(), departmentData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return departmentDataMap;
    }
}
